package com.kangyi.admin.controller;

import com.kangyi.admin.service.EntryManagementService;
import com.kangyi.dto.EntryManagementPageQueryDTO;
import com.kangyi.dto.RefuseApplicationDTO;
import com.kangyi.result.EntryManagementPageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.NursingHomeApplicationDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/entry")
@Slf4j
@Api(tags = "超级管理员-入驻管理相关接口")
public class EntryManagementController {

    @Autowired
    private EntryManagementService entryManagementService;

    @GetMapping("/page")
    @ApiOperation("入驻分页查询")
    public Result<EntryManagementPageResult> pageQuery(EntryManagementPageQueryDTO entryManagementPageQueryDTO){
        log.info("入驻分页查询:{}",entryManagementPageQueryDTO);
        try {
            EntryManagementPageResult entryManagementPageResult = entryManagementService.pageQuery(entryManagementPageQueryDTO);
            return Result.success(entryManagementPageResult,"入驻分页查询");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除入驻")
    @CacheEvict(cacheNames = "application_", allEntries = true)
    public Result deleteRegistrationApplication(@RequestParam List<Integer> ids){
        log.info("批量删除入驻:{}",ids);
        try {
            entryManagementService.deleteRegistrationApplication(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("查询养老院入驻审核详情")
//    @Cacheable(cacheNames = "application_", key = "'adminDetail_'+#id")
    public Result<NursingHomeApplicationDetailVO> detailApplication(@PathVariable Integer id){
        log.info("查询养老院入驻审核详情：{}",id);
        try {
            NursingHomeApplicationDetailVO nursingHomeApplicationDetailVO = entryManagementService.detailApplication(id);
            return Result.success(nursingHomeApplicationDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/refuse")
    @ApiOperation("拒绝养老院入驻")
    @CacheEvict(cacheNames = "application_", allEntries = true)
    public Result refuseApplication(@RequestBody RefuseApplicationDTO refuseApplicationDTO){
        log.info("拒绝养老院入驻：{}",refuseApplicationDTO);
        try {
            entryManagementService.refuseApplication(refuseApplicationDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("拒绝成功！");
    }


    @PostMapping("/pass")
    @ApiOperation("同意养老院入驻")
    @CacheEvict(cacheNames = "application_", allEntries = true)
    public Result passApplication(Integer applicationId){
        log.info("同意养老院入驻");
        try {
            entryManagementService.passApplication(applicationId);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success("同意成功！");
    }

}
